package com.example.big_job.Adapter;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.big_job.DB.DBHelper;
import com.example.big_job.Dao.Dao;
import com.example.big_job.FoodActivity;
import com.example.big_job.MeauActivity;
import com.example.big_job.R;
import com.example.big_job.RegisterActivity;
import com.example.big_job.StoreActivity;

import java.util.ArrayList;
import java.util.List;

public class FoodAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int type_zero = 0;
    private final int type_one = 1;
    private Context context;
    private List<Integer> mDatas;
    private int i=0;

    public ContentValues values0 = new ContentValues();
    private ContentValues values1 = new ContentValues();
    private ContentValues values2 = new ContentValues();
    private ContentValues values3 = new ContentValues();
    private ContentValues values4 = new ContentValues();
    private ContentValues values5 = new ContentValues();
    private ContentValues values6 = new ContentValues();
    private ContentValues values7 = new ContentValues();
    private ContentValues values8 = new ContentValues();
    private ContentValues values9 = new ContentValues();




    public FoodAdapter(Context context, List<Integer> mDatas) {
        super();
        this.context = context;
        this.mDatas = mDatas;
    }




    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == type_zero) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_food_order, parent, false);
            return new foodorderViewHolder(view);
        }
        else{
            view= LayoutInflater.from(context).inflate(R.layout.item_food, parent, false);
            return new foodViewHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        DBHelper sqlite = new DBHelper(context, 1);
        SQLiteDatabase database = sqlite.getReadableDatabase();
        if(holder.getItemViewType()==type_zero){
            foodorderViewHolder foodorderViewHolder=(FoodAdapter.foodorderViewHolder) holder;
            //返回上一个界面
            foodorderViewHolder.B_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=null;
                intent=new Intent(view.getContext(), StoreActivity.class);
                view.getContext().startActivity(intent);
            }
        });

            //下单
            foodorderViewHolder.B_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (i==0){
                    Toast.makeText(context.getApplicationContext(), "您还未选择任何菜品", Toast.LENGTH_SHORT).show();
                }
                else{
                database.insert("food", null, values0);
                database.insert("food", null, values1);
                database.insert("food", null, values2);
                database.insert("food", null, values3);
                database.insert("food", null, values4);
                database.insert("food", null, values5);
                database.insert("food", null, values6);
                database.insert("food", null, values7);
                database.insert("food", null, values8);
                database.insert("food", null, values9);

                Toast.makeText(context.getApplicationContext(), "下单成功", Toast.LENGTH_SHORT).show();
                Intent intent=null;
                intent=new Intent(view.getContext(), MeauActivity.class);
                view.getContext().startActivity(intent);}
            }
        });
        }
        else {

            foodViewHolder foodviewHolder = (FoodAdapter.foodViewHolder) holder;

            if (position == 1) {
                foodviewHolder.textfoodname.setText("豆腐抱蛋");
                foodviewHolder.foodprize.setText("90");
                foodviewHolder.foodrenqi.setText("人气：❤❤❤❤❤");
                String foodname = foodviewHolder.textfoodname.getText().toString();
                String prize = foodviewHolder.foodprize.getText().toString();

                String storeaddress=Dao.getStoreaddress();
                Log.i("store",storeaddress);
                foodviewHolder.add.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (compoundButton.isChecked()){
                            i++;
                            values0.put("foodName",foodname);
                            values0.put("prize",prize);
                        }

                    }
                });



            }
            if (position == 2) {
                foodviewHolder.textfoodname.setText("花菜红烧肉");
                foodviewHolder.foodprize.setText("80");
                foodviewHolder.foodrenqi.setText("人气：❤❤❤❤");
                String foodname = foodviewHolder.textfoodname.getText().toString();
                String prize = foodviewHolder.foodprize.getText().toString();

                foodviewHolder.add.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (compoundButton.isChecked()){
                            i++;
                            values1.put("foodName",foodname);
                            values1.put("prize",prize);
                        }

                    }
                });
            }
            if (position == 3) {
                foodviewHolder.textfoodname.setText("芹菜肉丝");
                foodviewHolder.foodprize.setText("70");
                foodviewHolder.foodrenqi.setText("人气：❤❤❤");
                String foodname = foodviewHolder.textfoodname.getText().toString();
                String prize = foodviewHolder.foodprize.getText().toString();

                foodviewHolder.add.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (compoundButton.isChecked()){
                            i++;
                            values2.put("foodName",foodname);
                            values2.put("prize",prize);
                        }

                    }
                });
            }
            if (position == 4) {
                foodviewHolder.textfoodname.setText("小炒肉");
                foodviewHolder.foodprize.setText("80");
                foodviewHolder.foodrenqi.setText("人气：❤❤❤❤");
                String foodname = foodviewHolder.textfoodname.getText().toString();
                String prize = foodviewHolder.foodprize.getText().toString();

                foodviewHolder.add.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (compoundButton.isChecked()){
                            i++;
                            values3.put("foodName",foodname);
                            values3.put("prize",prize);
                        }

                    }
                });
            }
            if (position == 5) {
                foodviewHolder.textfoodname.setText("花菜抱蛋");
                foodviewHolder.foodprize.setText("60");
                foodviewHolder.foodrenqi.setText("人气：❤❤❤");
                String foodname = foodviewHolder.textfoodname.getText().toString();
                String prize = foodviewHolder.foodprize.getText().toString();

                foodviewHolder.add.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (compoundButton.isChecked()){
                            i++;
                            values4.put("foodName",foodname);
                            values4.put("prize",prize);
                        }

                    }
                });
            }
            if (position == 6) {
                foodviewHolder.textfoodname.setText("香干豆芽");
                foodviewHolder.foodprize.setText("50");
                foodviewHolder.foodrenqi.setText("人气：❤❤❤❤");
                String foodname = foodviewHolder.textfoodname.getText().toString();
                String prize = foodviewHolder.foodprize.getText().toString();

                foodviewHolder.add.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (compoundButton.isChecked()){
                            i++;
                            values5.put("foodName",foodname);
                            values5.put("prize",prize);
                        }

                    }
                });
            }
            if (position == 7) {
                foodviewHolder.textfoodname.setText("猪肉抱蛋");
                foodviewHolder.foodprize.setText("90");
                foodviewHolder.foodrenqi.setText("人气：❤❤❤❤");
                String foodname = foodviewHolder.textfoodname.getText().toString();
                String prize = foodviewHolder.foodprize.getText().toString();

                foodviewHolder.add.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (compoundButton.isChecked()){
                            i++;
                            values6.put("foodName",foodname);
                            values6.put("prize",prize);
                        }

                    }
                });
            }
            if (position == 8) {
                foodviewHolder.textfoodname.setText("大锅猪肉");
                foodviewHolder.foodprize.setText("40");
                foodviewHolder.foodrenqi.setText("人气：❤❤❤❤❤");
                String foodname = foodviewHolder.textfoodname.getText().toString();
                String prize = foodviewHolder.foodprize.getText().toString();

                foodviewHolder.add.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (compoundButton.isChecked()){
                            i++;
                            values7.put("foodName",foodname);
                            values7.put("prize",prize);
                        }

                    }
                });
            }
            if (position == 9) {
                foodviewHolder.textfoodname.setText("蘑菇炖肉");
                foodviewHolder.foodprize.setText("70");
                foodviewHolder.foodrenqi.setText("人气：❤❤❤");
                String foodname = foodviewHolder.textfoodname.getText().toString();
                String prize = foodviewHolder.foodprize.getText().toString();

                foodviewHolder.add.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (compoundButton.isChecked()){
                            i++;
                            values8.put("foodName",foodname);
                            values8.put("prize",prize);
                        }

                    }
                });
            }
            if (position == 10) {
                foodviewHolder.textfoodname.setText("红烧大虾");
                foodviewHolder.foodprize.setText("100");
                foodviewHolder.foodrenqi.setText("人气：❤❤❤❤");
                String foodname = foodviewHolder.textfoodname.getText().toString();
                String prize = foodviewHolder.foodprize.getText().toString();

                foodviewHolder.add.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (compoundButton.isChecked()){
                            i++;
                            values9.put("foodName",foodname);
                            values9.put("prize",prize);
                        }

                    }
                });
            }
            foodviewHolder.imageViewcai.setImageResource(mDatas.get(position));
        }


    }



    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return type_zero;
        } else {
            return type_one;
        }
    }



    @Override
    public int getItemCount() {

        return mDatas.size();
    }
    public class foodorderViewHolder extends RecyclerView.ViewHolder{
        private Button B_fanhui;
        private Button B_order;
        public foodorderViewHolder(@NonNull View itemView) {
            super(itemView);
            B_fanhui=itemView.findViewById(R.id.b_fanhui);
            B_order=itemView.findViewById(R.id.b_xiadan);
        }
    }
    public class foodViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageViewcai;
        private TextView textfoodname;
        private TextView foodprize;
        private TextView foodrenqi;
        private CheckBox add;





        public foodViewHolder(@NonNull View itemView) {
            super(itemView);
            imageViewcai=itemView.findViewById(R.id.I_cai);
            textfoodname=itemView.findViewById(R.id.foodname);
            foodprize=itemView.findViewById(R.id.prize);
            foodrenqi=itemView.findViewById(R.id.t_renqi);
            add=itemView.findViewById(R.id.b_add);


        }

    }
}
